﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib.Filters;
using GestionePolveriLib.BL.FilterBags;
using GestionePolveriLib.BL;
using GestionePolveriLib.Utilities;

namespace GestionePolveri2.Filters
{
    public partial class VerniciatoreScortaMinimaFilter : VerniciatoreScortaMinimaFilterBase
    {
        public VerniciatoreScortaMinimaFilter()
        {
            InitializeComponent();

            if (!DesignMode)
            {
                this.EnableDatesChooser = false;

                RefreshData();

                this.FromDate = DateTime.Now;
                this.ToDate = DateTime.Now;

                this.rbPolveriAll.Tag = PolvereTypes.Tutte;
                this.rbPolveriNormali.Tag = PolvereTypes.Normali;
                this.rbPolveriXXXX.Tag = PolvereTypes.XXXX;

                this.rbPolveriAll.Checked = true;
            }
        }

        protected override void OnResetFilter(EventArgs e)
        {
            base.OnResetFilter(e);

            FilterBag.CodicePolvere = null;
            FilterBag.Fornitore = null;
            FilterBag.Tipo = PolvereTypes.Tutte;

            this.cmbFornitori.SelectedIndex = 0;
            this.cmbPolveri.SelectedIndex = 0;
        }

        protected override void OnExecuteFilter(EventArgs e)
        {
            if (this.cmbPolveri.SelectedItem == null)
                this.cmbPolveri.SelectedIndex = 0;
            if (this.cmbFornitori.SelectedItem == null)
                this.cmbFornitori.SelectedIndex = 0;

            FilterBag.Tipo = (PolvereTypes)(rbPolveriAll.Checked ? rbPolveriAll.Tag : (rbPolveriNormali.Checked ? rbPolveriNormali.Tag : rbPolveriXXXX.Tag));
            FilterBag.CodicePolvere = (((Polvere)this.cmbPolveri.SelectedItem).ID == -1 ? null : ((Polvere)this.cmbPolveri.SelectedItem).CodicePolvere);
            FilterBag.Fornitore = (((Fornitore)this.cmbFornitori.SelectedItem).ID == -1 ? null : ((Fornitore)this.cmbFornitori.SelectedItem).Descrizione);
            FilterBag.PeriodFrom = this.FromDate;
            FilterBag.PeriodTo = this.ToDate;
            FilterBag.UseDates = this.UseDatesFilter;

            if (
                (this.cmbFornitori.SelectedIndex == 0) &&
                (this.cmbPolveri.SelectedIndex == 0) &&
                (FilterBag.Tipo == PolvereTypes.Tutte) &&
                (FilterBag.Fornitore == null) &&
                (FilterBag.CodicePolvere == null) &&
                (FilterBag.UseDates == false)
              )
            {
                OnResetFilter(e);
                this.OnFilterButtonClick();
            }
            else
            {
                base.OnExecuteFilter(e);
            }

            //base.OnExecuteFilter(e);
        }

        private void LoadPolveri()
        {
            FilterComboBoxHelper.SetupPolveriComboBox(this.cmbPolveri);

            //List<Polvere> m_data = (List<Polvere>)FrameworkLib.Utilities.Singleton<GestionePolveriLib.Cache.LookUpData>.Instance.GetData("polveri");
            //if (m_data != null)
            //{
            //    m_data.Insert(0, new Polvere() { CodicePolvere = "Seleziona una polvere", ID = -1 });

            //    this.cmbPolveri.DisplayMember = "codicepolvere";
            //    this.cmbPolveri.ValueMember = "id";
            //    this.cmbPolveri.DataSource = m_data;
            //}
        }

        private void LoadFornitori()
        {
            FilterComboBoxHelper.SetupFornitoriComboBox(this.cmbFornitori);

            //List<Fornitore> m_data = (List<Fornitore>)FrameworkLib.Utilities.Singleton<GestionePolveriLib.Cache.LookUpData>.Instance.GetData("fornitori");
            //if (m_data != null)
            //{
            //    m_data.Insert(0, new Fornitore() { Descrizione = "Seleziona un fornitore", ID = -1 });

            //    this.cmbFornitori.DisplayMember = "Descrizione";
            //    this.cmbFornitori.ValueMember = "ID";
            //    this.cmbFornitori.DataSource = m_data;
            //}
        }

        public override void RefreshData()
        {
            LoadPolveri();
            LoadFornitori();
        }
    }

    public class VerniciatoreScortaMinimaFilterBase : FilterContainerBase2<VerniciatoreScortaMinimaFilterBag> { }
}
